/*
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 **
 **     http://www.apache.org/licenses/LICENSE-2.0
 **
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */

package org.geohunter.activity.map;

import com.google.android.maps.MapController;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;

import org.geohunter.GeoFixProvider;
import org.geohunter.GeocacheFilter;
import org.geohunter.Geocache;
import org.geohunter.menuactions.MenuActions;
import org.geohunter.task.ITaskRunner;
import org.geohunter.activity.main.GuiState;
import org.geohunter.activity.main.TabBase;

import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;

public class MapTab extends TabBase {
    
    private static final int DEFAULT_ZOOM_LEVEL = 14;

    private final GuiState mGuiState;
    private final MyLocationOverlay mMyLocationOverlay;
    private final OverlayManager<Overlay> mOverlayManager;
    private final MapController mMapController;
    private final GeoMapView mGeoMapView;
    private final CacheHint mCacheHint;
    private final ITaskRunner mTaskRunner;
    private final GeoFixProvider mGeoFixProvider;
    
    public MapTab(GuiState guiState, MenuActions menuActions,
            MyLocationOverlay myLocationOverlay,
            OverlayManager<Overlay> overlayManager, MapController mapController,
            View contentView, Drawable icon, GeoMapView geoMapView,
            CacheHint cacheHintHolder, ITaskRunner taskRunner,
            GeoFixProvider geoFixProvider) {
        super("tab_map", "Map", contentView, icon, menuActions);
        mGuiState = guiState;
        mMyLocationOverlay = myLocationOverlay;
        mOverlayManager = overlayManager;
        mMapController = mapController;
        mGeoMapView = geoMapView;
        mCacheHint = cacheHintHolder;
        mTaskRunner = taskRunner;
        mGeoFixProvider = geoFixProvider;
    }

    @Override
    public void onPause() {
        Log.d("geohunter", "MapTab.onPause()");
        mGeoMapView.setVisibility(View.INVISIBLE);
        mMyLocationOverlay.disableMyLocation();
        mMyLocationOverlay.disableCompass();
        if(mMyLocationOverlay instanceof FixedMyLocationOverlay)
            mGeoFixProvider.removeObserver((FixedMyLocationOverlay)mMyLocationOverlay);
        mTaskRunner.abort();
    }

    @Override
    public void onCreate() {
        Log.d("geohunter", "MapTab.onCreate()");
        mMapController.setZoom(DEFAULT_ZOOM_LEVEL);
        mOverlayManager.setCacheFilter(mGuiState.getActiveFilter());
        mOverlayManager.initArea();
    }
    
    @Override
    public void onResume() {
        Log.d("geohunter", "MapTab.onResume()");
        mMyLocationOverlay.enableMyLocation();
        mMyLocationOverlay.enableCompass();
        mGeoMapView.setVisibility(View.VISIBLE);
        if(mMyLocationOverlay instanceof FixedMyLocationOverlay)
            mGeoFixProvider.addObserver((FixedMyLocationOverlay)mMyLocationOverlay);


        Geocache prevSelected = mOverlayManager.getSelectedGeocache();
        Geocache selected = mGuiState.getActiveGeocache();
        if (prevSelected != selected) {
            mCacheHint.showGeocache(selected);
            if (selected != null) {
                Log.d("geohunter", "Setting selected to " + selected.getId());
                mMapController.animateTo(selected.getGeoPoint());
            }
            mOverlayManager.setSelectedGeocache(selected);
        }

        mOverlayManager.refreshIfNeeded();
        mGeoMapView.updateIfMoved();
    }
    
    @Override
    public void onDataViewChanged(GeocacheFilter filter, boolean isTabActive) {
        mOverlayManager.databaseChanged();
        mOverlayManager.setCacheFilter(filter);
        mOverlayManager.invalidate();
        if (isTabActive) {
            mOverlayManager.refreshIfNeeded();
        }
    }    
}
